package com.hao.cdk.sm.core.mabatisplus;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @author: 许自豪
 * @date: 2025-01-23 17:18:59
 * @description: MybatisPlus自动注入
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    public MyMetaObjectHandler() {
    }

    public void insertFill(MetaObject metaObject) {
        this.setInsertFieldValByName("status", "0", metaObject);
        this.setInsertFieldValByName("deleted", 0, metaObject);
        this.setInsertFieldValByName("createTime", new Date(), metaObject);
        this.setInsertFieldValByName("updateTime", new Date(), metaObject);
        this.setInsertFieldValByName("orderNum", 0, metaObject);
        this.setInsertFieldValByName("parentId", "0", metaObject);
        // SysUser user = this.getUser();
        // if (user != null) {
        //     this.setInsertFieldValByName("createUserId", user.getUserId(), metaObject);
        //     this.setInsertFieldValByName("createUserName", user.getUserName(), metaObject);
        //     this.setInsertFieldValByName("createOrgId", user.getOrgId(), metaObject);
        //     this.setInsertFieldValByName("createOrgNo", user.getOrgNo(), metaObject);
        //     this.setInsertFieldValByName("createOrgName", user.getOrgName(), metaObject);
        //     this.setInsertFieldValByName("createDeptId", user.getDeptId(), metaObject);
        //     this.setInsertFieldValByName("createDeptName", user.getDeptName(), metaObject);
        //     this.setInsertFieldValByName("updateUserId", user.getUserId(), metaObject);
        //     this.setInsertFieldValByName("updateUserName", user.getUserName(), metaObject);
        //     this.setInsertFieldValByName("updateOrgId", user.getOrgId(), metaObject);
        //     this.setInsertFieldValByName("updateOrgNo", user.getOrgNo(), metaObject);
        //     this.setInsertFieldValByName("updateOrgName", user.getOrgName(), metaObject);
        //     this.setInsertFieldValByName("updateDeptId", user.getDeptId(), metaObject);
        //     this.setInsertFieldValByName("updateDeptName", user.getDeptName(), metaObject);
        // }

    }

    public MetaObjectHandler setInsertFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject) {
        if ("status".equals(fieldName) && metaObject.hasGetter(fieldName)) {
            Class<?> statusFieldClass = metaObject.getGetterType(fieldName);
            if (statusFieldClass == Integer.class) {
                fieldVal = 0;
            }
        }

        return (MetaObjectHandler)(Objects.isNull(this.getFieldOriginalValue(fieldName, fieldVal, metaObject)) ? this.setFieldValByName(fieldName, fieldVal, metaObject, FieldFill.INSERT) : this);
    }

    public MetaObjectHandler setUpdateFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject) {
        return (MetaObjectHandler)(Objects.isNull(this.getFieldOriginalValue(fieldName, fieldVal, metaObject)) ? this.setFieldValByName(fieldName, fieldVal, metaObject, FieldFill.UPDATE) : this);
    }

    public void updateFill(MetaObject metaObject) {
        this.setUpdateFieldValByName("updateTime", new Date(), metaObject);
        // SysUser user = this.getUser();
        // if (user != null) {
        //     this.setUpdateFieldValByName("updateUserId", user.getUserId(), metaObject);
        //     this.setUpdateFieldValByName("updateUserName", user.getUserName(), metaObject);
        //     this.setUpdateFieldValByName("updateOrgId", user.getOrgId(), metaObject);
        //     this.setUpdateFieldValByName("updateOrgName", user.getOrgName(), metaObject);
        //     this.setUpdateFieldValByName("updateDeptId", user.getDeptId(), metaObject);
        //     this.setUpdateFieldValByName("updateDeptName", user.getDeptName(), metaObject);
        // }

    }

    private Object getFieldOriginalValue(String fieldName, Object fieldVal, MetaObject metaObject) {
        if (Objects.nonNull(fieldVal)) {
            if (metaObject.hasGetter(fieldName)) {
                return metaObject.getValue(fieldName);
            }

            if (metaObject.hasGetter("et")) {
                Object et = metaObject.getValue("et");
                if (et != null) {
                    MetaObject etMeta = SystemMetaObject.forObject(et);
                    if (etMeta.hasSetter(fieldName)) {
                        return etMeta.getValue(fieldName);
                    }
                }
            }
        }

        return null;
    }

    // private SysUser getUser() {
    // }
}
